% file.pro  -- Ruby on Rails style file references

:- module(file,
      [
        '__FILE__'/1,       % ?File
        dirname/2,          % +File,-Directory
        join/3,             % +X,+Y,-XY

        pathfile/3          % +LibraryPath,+Filename,-PathFile
      ]).


:- multifile
     user:file_search_path/2.

/*
 *  File object
 */


%    '__FILE__'(?File)
%
%    Convenience predicate for Rails notational compatibility
%    Corresponds to name of current source file
%
'__FILE__'(File) :-
     prolog_load_context(file, File).


%    dirname(+File, -Directory)
%
%    Mimmicks Rails'  File.dirname(__FILE__) function invocation
%
dirname('__FILE__', Directory) :- !,
     prolog_load_context(directory, Directory).

dirname(File, Directory) :-
     file_directory_name(File, Directory).


%    join(+X, +Y, -XY)
%
%    Mimmicks Rails'  File.join function
%
join(X, Y, XY) :-
     atom_concat(_, '/', X), !,
     atom_concat(X, Y, XY).
join(X, Y, XY) :-
     concat_atom([X, '/', Y], XY).


%   pathfile(+LibraryName, +FileName, -PathFile)
%
%   Builds absolute file path based on a library path (e.g. kernel_out)
%
pathfile(LibraryName, FileName, PathFile) :-
    user:file_search_path(LibraryName, Path),
    join(Path, FileName, PathFile).